package com.example.t1;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import static java.lang.Integer.*;

public class rain_activity extends AppCompatActivity {

    Button but_back;
    Button but_connect;
    ProgressBar p_rain;
    ProgressBar P_LIGHT;
    public  boolean indisconect = true;
    Button but_close;
    Button open_light;
    Button close_light;
    TextView rec1;
    TextView rec2;
    EditText RrceiveEditText;//定义信息输入框
    Handler mainHandler = null;
    Handler mainHandler2 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rain);
        open_light  = findViewById(R.id.but_openli);
        close_light = findViewById(R.id.but_closedli);
        rec1 = findViewById(R.id.rec);
        rec2= findViewById(R.id.recli);
        p_rain = findViewById(R.id.prain1);
        P_LIGHT = findViewById(R.id.plight1);
        but_close = findViewById(R.id.but_closed);
        but_back = findViewById(R.id.but_back);
        but_connect = findViewById(R.id.but_connect);

        mainHandler2 = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //                                    int rain_n;
//                                  rain_n = parseInt(rcvMsg);

                P_LIGHT.setProgress(msg.arg2);
                // mText.setText("从子线程接收到的消息为:" + msg.arg1);
            }
        };

        mainHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //                                    int rain_n;
//                                  rain_n = parseInt(rcvMsg);
                p_rain.setProgress(msg.arg1);

               // mText.setText("从子线程接收到的消息为:" + msg.arg1);
            }
        };
        close_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {//System.out.println("33");
                        closeli_Server();
                    }
                }.start();

            }
            private void closeli_Server() {

                Socket socket = null;
                try {

                    socket = new Socket("bemfa.com", 8340);
                    //2.获取输出流，向服务器端发送信息

                    OutputStream os = socket.getOutputStream();//字节输出流
                    PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
                    pw.write("cmd=2&uid=af5f58e847008d7b19221a66b7af3a53&topic=aa&msg=4\r\n");
                    pw.flush();
                    socket.shutdownOutput();//关闭输出流
                    socket.close();
                } catch (IOException e) {

                }
            }
        });
        open_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {//System.out.println("33");
                        openli_Server();
                    }
                }.start();

            }
            private void openli_Server() {

                Socket socket = null;
                try {

                    socket = new Socket("bemfa.com", 8340);
                    //2.获取输出流，向服务器端发送信息

                    OutputStream os = socket.getOutputStream();//字节输出流
                    PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
                    pw.write("cmd=2&uid=af5f58e847008d7b19221a66b7af3a53&topic=aa&msg=3\r\n");
                    pw.flush();
                    socket.shutdownOutput();//关闭输出流
                    socket.close();
                } catch (IOException e) {

                }
            }
        });
        but_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {//System.out.println("33");
                        closeServer();
                    }
                }.start();

            }
            private void closeServer() {

                Socket socket = null;
                try {

                    socket = new Socket("bemfa.com", 8340);
                    //2.获取输出流，向服务器端发送信息

                    OutputStream os = socket.getOutputStream();//字节输出流
                    PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
                    pw.write("cmd=2&uid=af5f58e847008d7b19221a66b7af3a53&topic=aa&msg=2\r\n");
                    pw.flush();
                    socket.shutdownOutput();//关闭输出流
                    socket.close();
                } catch (IOException e) {

                }
            }
        });
        but_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {//System.out.println("33");
                        acceptServer();
                    }
                }.start();


            }

            private void acceptServer() {

                Socket socket = null;
                try {

                   socket = new Socket("bemfa.com", 8340);
                    //2.获取输出流，向服务器端发送信息

                   OutputStream os = socket.getOutputStream();//字节输出流
                    PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
                   pw.write("cmd=2&uid=af5f58e847008d7b19221a66b7af3a53&topic=aa&msg=1\r\n");
                   pw.flush();
                   socket.shutdownOutput();//关闭输出流
                   socket.close();
                } catch (IOException e) {

               }
            }
        });


        but_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rain_activity.this.finish();
            }
        });

        class MyThread extends Thread {
            Socket socket = null;        //定义socket
            OutputStream outputStream = null;    //定义输出流（发送）
            InputStream inputStream = null;    //定义输入流（接收）
            private DataInputStream dis;
            public String rcvMsg;
            //public     int rain_n;
            private String TAG = "TcpClient";
            //TextView recdis;
            MainActivity ma;
            private BufferedReader bufferedReader = null;
            public void run() {
                try {
                    socket = new Socket("bemfa.com", 8340);
                    //  socket = new Socket("10.22.242.233", 6666);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                //获取输出流
                try {

                    outputStream = socket.getOutputStream();
                    PrintWriter pw = new PrintWriter(outputStream);//将输出流包装为打印流
                    pw.write("cmd=1&uid=af5f58e847008d7b19221a66b7af3a53&topic=RAIN\r\n");
                    pw.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                byte[] buffer = new byte[1024];//创建接收缓冲区
                try {
                    while (true) {
                        inputStream = socket.getInputStream();
                        dis = new DataInputStream(inputStream);

                      int len = dis.read(buffer);//数据读出来，并且返回数据的长度

                        if (len > 13) {
                            System.out.println(len);
                            //rcvMsg = new String(buffer, 10, len-12, "utf-8");
                             rcvMsg = new String(buffer, 58, len-60, "utf-8");
                            Log.i(TAG, "run: 收到消息:" + rcvMsg);
                            super.run();
                            Message msg = Message.obtain();
                            int rain_n =  Integer.parseInt(rcvMsg);
                            msg.arg1 = rain_n;


                            mainHandler.sendMessage(msg);

                        }

                        runOnUiThread(new Runnable()//不允许其他线程直接操作组件，用提供的此方法可以
                        {
                            public void run()
                            {
                                if (len > 0) {
                                    rec1.setText(rcvMsg);
                                }
                            }
                        });


                    }

                }
                catch (IOException e) {
                    System.out.print(7);


                }




            }
        }


        class MyThread2 extends Thread {
            Socket socket = null;        //定义socket
            OutputStream outputStream = null;    //定义输出流（发送）
            InputStream inputStream = null;    //定义输入流（接收）
            private DataInputStream dis;
            public String rcvMsg;
            //public     int rain_n;
            private String TAG = "TcpClient";
            //TextView recdis;
            MainActivity ma;
            private BufferedReader bufferedReader = null;
            public void run() {
                try {
                    socket = new Socket("bemfa.com", 8340);
                    //  socket = new Socket("10.22.242.233", 6666);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                //获取输出流
                try {

                    outputStream = socket.getOutputStream();
                    PrintWriter pw = new PrintWriter(outputStream);//将输出流包装为打印流
                    pw.write("cmd=1&uid=af5f58e847008d7b19221a66b7af3a53&topic=LIGHT\r\n");
                    pw.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                byte[] buffer = new byte[1024];//创建接收缓冲区
                try {
                    while (true) {
                        inputStream = socket.getInputStream();
                        dis = new DataInputStream(inputStream);

                        int len = dis.read(buffer);//数据读出来，并且返回数据的长度

                        if (len > 13) {
                            System.out.println(len);
                            // rcvMsg = new String(buffer, 10, len-12, "utf-8");
                            rcvMsg = new String(buffer, 59, len-61, "utf-8");
                            Log.i(TAG, "run: 收到消息:" + rcvMsg);
                            super.run();
                            Message msg = Message.obtain();
                            int LIGHT_N =  Integer.parseInt(rcvMsg);
                            msg.arg2 = LIGHT_N;


                            mainHandler2.sendMessage(msg);

                        }

                        runOnUiThread(new Runnable()//不允许其他线程直接操作组件，用提供的此方法可以
                        {
                            public void run()
                            {
                                if (len > 0) {
                                    rec2.setText(rcvMsg);
                                }
                            }
                        });


                    }

                }
                catch (IOException e) {
                    System.out.print(7);


                }




            }
        }
        MyThread a;
        a = new MyThread();
        //rec1.setText("接收区");

        MyThread2 a2;
        a2 = new MyThread2();
        //rec1.setText("接收区");
        a2.start();
        a.start();
    }


}
